package cn.mybatis.plugin.conf;

import cn.mybatis.plugin.customizer.MyCustomizer;
import cn.mybatis.plugin.pagehelper.MySqlPageSqlDialect;
import cn.mybatis.plugin.pagehelper.PageInterceptor;
import cn.mybatis.plugin.pagehelper.PageSqlDialectHolder;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Locale;

/**
 * @author: 张澎_9970
 * @date: 2022.1.17 下午 11:26
 */
@Configuration
public class PluginConfig {

    @Bean
    public MyCustomizer ccc() {
        return configuration -> configuration.addInterceptor(new PageInterceptor(null));

    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer(DataSource dataSource) {
        String driverClassName = ((DruidDataSourceWrapper) dataSource).getDriverClassName();
        if (!driverClassName.toLowerCase(Locale.ROOT).contains("mysql")) {
            throw new RuntimeException("暂不支持该数据库");
        } else {
            PageSqlDialectHolder.setPageSqlDialect(new MySqlPageSqlDialect());
        }

        return configuration -> configuration.addInterceptor(new PageInterceptor(dataSource));
    }

}
